<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 注意: 先引入 react基础库,再引入react dom解析库 -->
  <script src="lib/react/react.development.js"></script>
  <script src="lib/react/react-dom.development.js"></script>
  <script src="lib/babel.min.js"></script>
</head>
<body>
  <!-- 创建react解析的容器 -->
  <div id="root"></div>
  <script type="text/babel">
    
    class Warning extends React.Component {
      constructor(props){
        super(props);
      }
      render(){
        // 阻止渲染
        if(!this.props.isWarning){
          return null;
        }
        return (
          <div style={{color: 'orange'}}>这是警告!</div>
        )
      }
    }
    class Foo extends React.Component {
      render(){
        return (
          <div>hello,foo!</div>
        )
      }
    }
    class Bar extends React.Component {
      render(){
        return (
          <div>hello,bar!</div>
        )
      }
    }
    class HelloWorld extends React.Component {
      constructor(props){
        super(props);
        // 声明可变状态 isShow
        this.state = {
          isShow : false
        }
        // 必须bind事件
        this.toggleWarning = this.toggleWarning.bind(this);
      }
      toggleWarning(){
        //这里每次切换依赖于之前的状态,所以用回调形式写
        this.setState((state,props)=>({
          isShow: !state.isShow
        }))
      }
      render(){
        return (
          <div>
            <Warning isWarning={this.state.isShow}/>
            <div>hello,world!</div>
            {this.props.isFoo && <Foo/>}
            <hr />
            { /* 三元表达式 */}
            {this.props.isBar ? <Bar/>: <Foo/>}
            <button onClick={this.toggleWarning}>{this.state.isShow?'关闭警告':'开启警告'}</button>
            {/*换种形式? 实现方式是多样的,这里只是演示了各种形式,具体需要根据自己的实现逻辑来判断*/}
            {this.state.isShow && <Warning isWarning={true}/>}
          </div>
        )
      }
    }
    // 使用react 相关api 渲染到容器
    ReactDOM.render(<HelloWorld isFoo={true} isBar={false}/>,document.getElementById('root'));
  </script>
</body>
</html>